public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
}
string FileName = "";
string Content = "";
private void button1_Click(object sender, EventArgs e)
{
listView1.Items.Clear();
string[] Disk= Directory.GetLogicalDrives();
string path = textBox3.Text;
FileName = "";
Content = "";
if (textBox1.Text != "")
{
FileName = textBox1.Text;
}
if (textBox2.Text != "")
{
Content = textBox2.Text;
}
if (textBox1.Text == "" && textBox2.Text == "")
{
return;
}
if (path != "")
{
Disk = Directory.GetDirectories(path "\\");
}
DirList.Clear();
foreach (string ddd in Disk)
{
GetAllDir(ddd);
}
}
List<string> DirList = new List<string>();
private int DirInt = 0;
private void GetAllDir(string b)
{
try
{
string[] c = Directory.GetDirectories(b);
//////
foreach (string fi in Directory.GetFiles(b))
{
if (Directory.GetDirectoryRoot(b) != b) { break; }
FileInfo f = new FileInfo(fi);
if ((f.Name f.Extension).Contains(FileName) && FileName != "")
{
ListViewItem li = new ListViewItem();
li.Text = f.Name;
li.SubItems.Add(b);
li.SubItems.Add(Directory.GetLastWriteTime(f.FullName).ToString("yyyy-MM-dd HH:mm:ss"));
li.SubItems.Add((f.Length / 1024).ToString() "kb");
listView1.Items.Add(li);
}
if (f.Extension.ToLower() == ".cs" || f.Extension.ToLower() == ".txt" || f.Extension.ToLower() == ".ini" || f.Extension.ToLower() == ".config")
{
if (Content !="" && File.ReadAllText(f.FullName).ToLower().Contains(Content.ToLower()))
{
ListViewItem li = new ListViewItem();
li.Text = f.Name;
li.SubItems.Add(b);
li.SubItems.Add(Directory.GetLastWriteTime(f.FullName).ToString("yyyy-MM-dd HH:mm:ss"));
li.SubItems.Add((f.Length / 1024).ToString() "kb");
listView1.Items.Add(li);
}
}
}
//////
foreach (string d in c)
{
DirList.Add(d);
string e = Directory.GetParent(d).FullName "\\";
string x = d.Replace(e, "");
if (x.Contains(FileName) && FileName != "")
{
ListViewItem li = new ListViewItem();
li.Text = x;
li.SubItems.Add(d);
li.SubItems.Add(Directory.GetLastWriteTime(d).ToString("yyyy-MM-dd HH:mm:ss"));
li.SubItems.Add("");
listView1.Items.Add(li);
}
try
{
foreach (string fi in Directory.GetFiles(d))
{
FileInfo f = new FileInfo(fi);
if ((f.Name f.Extension).Contains(FileName) &&FileName !="")
{
ListViewItem li = new ListViewItem();
li.Text = f.Name;
li.SubItems.Add(d);
li.SubItems.Add(Directory.GetLastWriteTime(f.FullName).ToString("yyyy-MM-dd HH:mm:ss"));
li.SubItems.Add((f.Length/1024).ToString() "kb");
listView1.Items.Add(li);
}
if (f.Extension.ToLower() == ".cs" || f.Extension.ToLower() == ".txt" || f.Extension.ToLower() == ".ini" || f.Extension.ToLower() == ".config")
{
if ( Content !="" && File.ReadAllText(f.FullName).ToLower().Contains(Content.ToLower()))
{
ListViewItem li = new ListViewItem();
li.Text = f.Name;
li.SubItems.Add(d);
li.SubItems.Add(Directory.GetLastWriteTime(f.FullName).ToString("yyyy-MM-dd HH:mm:ss"));
li.SubItems.Add((f.Length / 1024).ToString() "kb");
listView1.Items.Add(li);
}
}
}
}
catch
{ }
label4.Text = DirList.Count.ToString();
Application.DoEvents();
GetAllDir(d);
}
}
catch
{ }
}
private void listView1_MouseClick(object sender, MouseEventArgs e)
{
int i = listView1.SelectedIndices[0];
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
string b= listView1.Items[i].SubItems[1].Text.ToString();
RunCmd2("explorer.exe", b);
}
}
static bool RunCmd2(string cmdExe, string cmdStr)
{
bool result = false;
try
{
using (Process myPro = new Process())
{
myPro.StartInfo.FileName = "cmd.exe";
myPro.StartInfo.UseShellExecute = false;
myPro.StartInfo.RedirectStandardInput = true;
myPro.StartInfo.RedirectStandardOutput = true;
myPro.StartInfo.RedirectStandardError = true;
myPro.StartInfo.CreateNoWindow = true;
myPro.Start();
//如果调用程序路径中有空格时,cmd命令执行失败,可以用双引号括起来 ,在这里两个引号表示一个引号(转义)
string str = string.Format(@"""{0}"" {1} {2}", cmdExe, cmdStr, "&exit");
myPro.StandardInput.WriteLine(str);
myPro.StandardInput.AutoFlush = true;
myPro.WaitForExit();
result = true;
}
}
catch
{
}
return result;
}
}
评论